/**
 * mkrand.c
 * @author Ulyan Sobin
 */

#include "mkrand.h"

#include <stdlib.h>
#include <stdint.h>
#include <time.h>

int mkrand(void* buffer, size_t size)
{
    uint8_t binary[256];
    uint32_t i, j, t;
    size_t si = 0;
    uint8_t *p;

    srand((uint32_t)time(NULL));

    for (i = 0; i < 256; i++) {
        binary[i] = i;
    }

    for (i = 0; i < 255; i++) {
        j = i + rand() % (256 - i);
        t = binary[i];
        binary[i] = binary[j];
        binary[j] = t;
    }

    for (si = 0, p = (uint8_t*)buffer; si + 256 < size; si += 256, p += 256) {
        for (i = 0; i < 255; i++) {
            j = i + rand() % (256 - i);
            t = binary[i];
            binary[i] = binary[j];
            binary[j] = t;
        }
        memcpy(p, binary, 256);
    }

    memcpy(p, binary, size - si);

    return 0;
}
